<?php


namespace app\common\model;


use think\Model;

class Company extends Model
{
    /**
     * 获取企业列表
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public static function lists($params){
        $where = [];

        if ($params['industry'] > 0) {
            $where['industry'] = $params['industry'] - 1;
        }
        if ($params['financing'] > 0) {
            $where['financing'] = $params['financing'] - 1;
        }
        if ($params['scale'] > 0) {
            $where['scale'] = $params['scale'] - 1;
        }

        // 查询已审核过的企业列表  共16条
        $data = self::alias('c')
            ->order('c.id desc')
            ->where($where)
            ->where('c.status',2)
            ->field('id,company_name,industry,financing,logo,short_name,scale')
            ->paginate(16);

        foreach ($data as $key => $item) {
            $job = Job::where('company_id',$item['id'])->where('status',2)->field('id,title,salary')->find();
            $data[$key]['financing'] = financingtype($item['financing']);
            $data[$key]['scale'] = scaletype($item['scale']);

            if ($job) {
                $data[$key]['job_id'] = $job['id'];
                $data[$key]['title'] = $job['title'];
                $data[$key]['salary'] = salary($job['salary']);
            }
        }

        return ['list'=>$data];
    }

    /**
     * 获取公司信息
     */
    public static function getinfo($id)
    {
        $company = self::where('id',$id)->find()->toArray();

        $company['industry_text'] = $company['industry'];
        $company['financing_text'] = $company['financing'];
        $company['area_no_text'] = areaNoText($company['area_no']);
        if($company['welfare']) {
            $company['welfare'] = json_decode($company['welfare'], true);
        } else {
            $company['welfare'] = [];
        }

        return $company;
    }

    /**
     * 获取企业列表
     * @return [type] [description]
     */
    public function getlist()
    {
        $model = $this;
        // 查询首页展示数据
        $model = $model->where('status',2)->where('homestart',1);

        $model = $model->field('id,short_name,logo,industry,financing')->limit(8);

        // 关联统计职位数量
        $model = $model->withCount(['job' => function($query) {
            $query->where('status',2);
        }]);

        // 在线职位最多的排最前面
        $list = $model->order('update_time desc')->select()->toArray();

        foreach ($list as $key => $value) {
            $list[$key]['financing'] = financingtype($value['financing']);
        }

        return $list;
    }

    /**
     * 修改数据
     * @param $id
     * @param $post
     */
    public function editinfo($id,$post)
    {
        $this->where('id',$id)->save($post);
    }

    /**
     * 一对一关联 关联职位
     * @return [type] [description]
     */
    public function job()
    {
        return $this->hasOne(Job::class,'company_id','id');
    }

    /**
     * 行业
     * @param $value
     * @return string
     */
    public function getIndustryAttr($value)
    {
        return industrytype($value);
    }

    /**
     * 企业logo
     * @param  [type] $value [description]
     * @return [type]        [description]
     */
    public function getlogoAttr($value){
        if (!$value) {
            return "https://cdn.yangzhu360.com/static/images/zhaopin/companys.png";
        }
        return $value;
    }
}
